/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GestureRecognition;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.logging.Level;

/**
 *
 * @author Home
 */
public class Slider {

    static int frameCount = 0;
    static int samePoint = 0;
    final int slide_scroll_max = 200;
    private static int[] prevPoint = new int[2];
    final int MAX_SAME_POINT_X = 100;
    boolean slideDone = false;
    int tt = 0;
    final int MIN_SLIDE = 200;
    final int MAX_FRAME_COUNT = 10;
    int dxT, dyT, d1, d2;
    int[] dx = new int[MAX_FRAME_COUNT];
    int[] dy = new int[MAX_FRAME_COUNT];

    public Slider() {
        for (int i = 0; i < MAX_FRAME_COUNT - 2; i++) {
            dx[i] = 0;
            dy[i] = 0;
        }
    }
    boolean first = true;

    public int identifyGestureType(int[] currentPoint) {
        int ret = 0;
        //System.out.println("ddd");
        if (slideDone) {
            tt++;
            if (tt == 15) {
                // System.out.println("if");
                slideDone = false;
                tt = 0;
            } else {
                //System.out.println("else " + tt);
                return 0;
            }
        } else {

            //System.out.println("came here");
            dxT = 0;
            dyT = 0;
            for (int i = 0; i < MAX_FRAME_COUNT - 1; i++) {

                dx[i] = dx[i + 1];
                dy[i] = dy[i + 1];
                dxT = dxT + dx[i];
                dyT = dyT + dy[i];
            }

            if (first) {
                d1=0;
                d2=0;
                first=false;
            } else {
                d1 = currentPoint[0] - prevPoint[0];
                d2 = currentPoint[1] - prevPoint[1];
            }
            if (Math.abs(d1) > MIN_SLIDE + 200 || Math.abs(d2) > MIN_SLIDE + 200) {
                dxT = dyT = 0;
                d1 = d2 = 0;
                dx = new int[MAX_FRAME_COUNT];
                dy = new int[MAX_FRAME_COUNT];
            }
            dx[MAX_FRAME_COUNT - 1] = d1;
            dy[MAX_FRAME_COUNT - 1] = d2;


            if (d1 < MAX_SAME_POINT_X && d2 < MAX_SAME_POINT_X) {
                samePoint++;
                if (samePoint == 40) {
                    samePoint = 0;
                    dxT = dyT = 0;
                    d1 = d2 = 0;
                    dx = new int[MAX_FRAME_COUNT];
                    dy = new int[MAX_FRAME_COUNT];
                }
            }


            dxT = dxT + d1;
            dyT = dyT + d2;
            prevPoint[0] = currentPoint[0];
            prevPoint[1] = currentPoint[1];

            if (Math.abs(dxT) > MIN_SLIDE && Math.abs(dyT) < slide_scroll_max) {
                //System.out.println("dx1=" + dxT + " dy1=" + dyT);
                int dir = 1;
                if (dxT > 0) {
                    System.out.println("slide right");
                    ret = +1;
                    slide(ret);
                } else {
                    System.out.println("slide left");
                    ret = -1;
                    slide(ret);
                }
                dx = new int[MAX_FRAME_COUNT];
                dy = new int[MAX_FRAME_COUNT];

                dxT = 0;
                dyT = 0;
                first = true;
                slideDone = true;
            } else if (Math.abs(dxT) < slide_scroll_max && Math.abs(dyT) > MIN_SLIDE) {
                int dir = 1;
                //System.out.println("dx1=" + dxT + " dy1=" + dyT);
                if (dyT < 0) {
                    System.out.println("scroll up");
                    ret = -2;
                    slide(ret);
                } else {
                    System.out.println("scroll down");
                    ret = +2;
                    slide(ret);
                }
                dx = new int[MAX_FRAME_COUNT];
                dy = new int[MAX_FRAME_COUNT];
                dxT = 0;
                dyT = 0;
                first = true;
                slideDone = true;
            }

        }
        return ret;
    }

    public void slide(int direction) {
        try {
            Robot rb = new Robot();
            switch (direction) {
                case -1:
                    rb.keyPress(KeyEvent.VK_LEFT);
                    rb.keyRelease(KeyEvent.VK_LEFT);
                    break;
                case +1:
                    rb.keyPress(KeyEvent.VK_RIGHT);
                    rb.keyRelease(KeyEvent.VK_RIGHT);
                    break;
                case -2:
                    rb.keyPress(KeyEvent.VK_UP);
                    rb.keyRelease(KeyEvent.VK_UP);
                    break;
                case +2:
                    rb.keyPress(KeyEvent.VK_DOWN);
                    rb.keyRelease(KeyEvent.VK_DOWN);
                    break;
            }
        } catch (AWTException ex) {
            java.util.logging.Logger.getLogger(Slide.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
